package leetcode.题库.二叉树;

import leetcode.题库.pubclass.ListNode;
import leetcode.题库.pubclass.TreeNode;

/**
 * description:
 *
 * @author 颜真明
 * @date 2024/3/29  16:59
 */
public class Solution124 {
    static int res = Integer.MIN_VALUE;
    public static int maxPathSum(TreeNode root) {
        dfs(root);
        return res;
    }
    public static int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return root.val;
        }
        int left = Math.max(0, dfs(root.left));
        int right = Math.max(0, dfs(root.right));
        int val = left + right + root.val;
        res = Math.max(res, val);
        return Math.max(left, right) + root.val;
    }

    public static void main(String[] args) {
//        TreeNode node1 = new TreeNode(1);
//        TreeNode node2 = new TreeNode(2);
//        TreeNode node3 = new TreeNode(3);
//        node1.left = node2;
//        node1.right = node3;
//        System.out.println(maxPathSum(node1));

        TreeNode node4 = new TreeNode(-2);
        TreeNode node5 = new TreeNode(-1);
        node4.right = node5;
        System.out.println(maxPathSum(node4));

    }

}
